/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.news.presenter;

import com.yiqi.liebang.entity.bo.IssuesBo;
import com.yiqi.liebang.entity.bo.OtherUserId;
import com.yiqi.liebang.entity.bo.PeopleBo;
import com.yiqi.liebang.entity.bo.RelationshipBo;
import com.yiqi.liebang.feature.news.contract.IssuesContract;
import com.yiqi.liebang.feature.news.contract.RelationshipContract;
import com.yiqi.liebang.feature.news.dependencies.issues.DaggerIssuesPresenterComponent;
import com.yiqi.liebang.feature.news.dependencies.relationship.DaggerRelationshipPresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class RelationshipPresenter implements RelationshipContract.Presenter {

    private RelationshipContract.View mView;

    @Inject

    RelationshipContract.Model mModel;


    public RelationshipPresenter(RelationshipContract.View mView) {
        this.mView = mView;
        DaggerRelationshipPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void deleteFriend(String deleteUserId) {
        RelationshipBo relationshipBo = new RelationshipBo();
        relationshipBo.setFriendUid(deleteUserId);
        mModel.deleteFriend(relationshipBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showDeleteFriendSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void addBlack(String blackUserId) {
        RelationshipBo relationshipBo = new RelationshipBo();
        relationshipBo.setBlackUserUid(blackUserId);
        mModel.addBlack(relationshipBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showAddBlackSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void isFriend(String id) {
        mModel.isFriend(new OtherUserId(id)).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {

                mView.showIsFriendSuccess(s.equals("1")?true:false);
            }

            @Override
            public void onError(Throwable e) {

                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void commitAddFriend(String friendId) {
        PeopleBo relationshipBo = new PeopleBo();
        relationshipBo.setApplicationUserUid(friendId);
        mModel.commitAddFriend(relationshipBo).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showAddFriendSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
